package com.kk.xhr.admin.biz.impl;

import com.kk.xhr.admin.biz.IStatisticsService;
import com.kk.xhr.admin.controller.vo.SignDayVO;
import com.kk.xhr.admin.controller.vo.UserInfoVO;
import com.kk.xhr.model.dto.UserInfoDTO;
import com.kk.xhr.model.service.ISignService;
import com.kk.xhr.model.service.IUserService;
import com.kk.xhr.util.DateUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 统计服务接口实现
 *
 * @author kkmystery
 * @version 1.0 2021/3/29
 * @since 1.0.0
 */
@Service
@RequiredArgsConstructor
public class StatisticsServiceImpl implements IStatisticsService {
    private final ISignService signService;
    private final IUserService userService;

    @Override
    public List<Long> queryWeekSignRecord() {
        Pair<LocalDateTime, LocalDateTime> monAndSun = DateUtil.getWeek();
        return signService.queryWeekSign(monAndSun.getLeft(), monAndSun.getRight());
    }

    @Override
    public SignDayVO queryDaySignRecord() {
        List<UserInfoDTO> userList = userService.query();
        Set<Long> ids = signService.queryDaySigned(LocalDate.now());
        List<UserInfoVO> signed = new ArrayList<>();
        List<UserInfoVO> unSign = userList.stream()
            .filter(info -> {
                if (ids.contains(info.getId())) {
                    signed.add(UserInfoVO.builder()
                        .id(info.getId())
                        .name(info.getName())
                        .build());
                    return false;
                }
                return true;
            })
            .map(dto -> UserInfoVO.builder()
                .id(dto.getId())
                .name(dto.getName())
                .build())
            .collect(Collectors.toList());
        return SignDayVO.builder()
            .signed(signed)
            .unSign(unSign)
            .build();
    }
}
